//
//  StatusItemView.m
//  Statz
//
//  Created by Dave MacLachlan on 2007/11/30.
//  Copyright 2007 Google Inc. All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License.  You may obtain a copy
// of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
// License for the specific language governing permissions and limitations under
// the License.
//

#import "StatusItemView.h"
#import <Carbon/Carbon.h>

@implementation StatusItemView

- (id)initWithStatusItem:(NSStatusItem*)item {
  NSRect frame = NSMakeRect(0, 0, [item length], GetMBarHeight());
  if ((self = [super initWithFrame:frame])) {
    statusItem_ = item;
  }
  return self;
}

- (void)dealloc {
  [image_ release];
  [alternateImage_ release];
  [super dealloc];
}

- (void)drawRect:(NSRect)rect {
  [statusItem_ drawStatusBarBackgroundInRect:rect withHighlight:selected_];
  NSImage *imageToUse = nil;
  if (selected_ && alternateImage_) {
    imageToUse = alternateImage_;
  } else {
    imageToUse = image_;
  }
  NSSize imageSize = [imageToUse size];
  NSRect imageRect = NSMakeRect(0, 0, imageSize.width, imageSize.height);
  NSRect bounds = [self bounds];
  bounds = NSInsetRect(bounds, 3, 3);
  [imageToUse drawInRect:bounds fromRect:imageRect operation:NSCompositeSourceOver fraction:1.0f];
}

- (void)setImage:(NSImage*)image {
  if (image != image_) {
    [image_ release];
    image_ = [image retain];
    [self setNeedsDisplay:YES];
  }
}

- (void)setAlternateImage:(NSImage*)image {
  if (image != alternateImage_) {
    [alternateImage_ release];
    alternateImage_ = [image retain];
    [self setNeedsDisplay:YES];
  }
}

- (void)mouseDown:(NSEvent *)theEvent {
  [target_ performSelector:action_ withObject:self];
}

- (void)setAction:(SEL)action {
  action_ = action;
}

- (void)setTarget:(id)target {
  target_ = target;
}

- (void)setSelected:(BOOL)selected {
  if (selected_ != selected) {
    selected_ = selected;
    [self setNeedsDisplay:YES];
  }
}
    
@end
